package com.weedsdream.app.compose

import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.weedsdream.app.R
import com.weedsdream.app.colorplate.curRGB
import com.weedsdream.compose.Space
import com.weedsdream.fileselect.ImageSelector
import com.weedsdream.function.inCoroutine
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay

/**当前选中的工具*/
var curTool by mutableStateOf(ToolType.笔)

/**工具隐藏状态*/
var hidden by mutableStateOf(false)

/**工具类型*/
@Suppress("EnumEntryName", "NonAsciiCharacters")
enum class ToolType {
    笔, 橡皮擦, 选择, 更多, 换背景
}

/**按钮组*/
@Composable
internal fun Buttons() {
    Box(Modifier.fillMaxSize()) {
        Column(Modifier.padding(2.dp)) {
            if (!hidden) {
                Row {
                    listOf(BtnConfig(R.drawable.edit, curRGB, ToolType.笔), BtnConfig(
                        R.drawable.eraser, Color.DarkGray, ToolType.橡皮擦,
                        if (curTool == ToolType.橡皮擦) curRGB else Color.Black
                    ), BtnConfig(R.drawable.hand, Color.Black, ToolType.选择) {
                        curTool = ToolType.选择
                        chosenPaths.clear()
                        dragPos = false
                    }).forEach { it.Show() }
                }
                listOf(
                    BtnConfig(R.drawable.image, Color.Black, ToolType.换背景) {
                        ImageSelector.openPicture(
                            selectMimeType = SelectMimeType.TYPE_IMAGE,
                            selectModeConfig = SelectModeConfig.SINGLE
                        ) {
                            if (it.isNotEmpty()) {
                                bgImg = it[0]
                            }
                        }
                    },
                    BtnConfig(R.drawable.history, Color.DarkGray, ToolType.更多) {
                        moreUiOpen = true
                    },
                ).forEach { it.Show() }
            }
            Spacer(Modifier.weight(1f))
            Icon(
                painterResource(if (hidden) R.drawable.arrow_bs else R.drawable.arrow_te), null,
                Modifier
                    .padding(7.dp)
                    .size(30.dp)
                    .alpha(hiddenBtnAlpha)
                    .pointerInput(Unit) {
                        detectTapGestures {
                            hidden = !hidden
                            hiddenBtnAlpha = 1f
                            hiddenJob?.run { cancel() }
                            if (hidden) {
                                hiddenJob = inCoroutine {
                                    while (hiddenBtnAlpha > 0f) {
                                        delay(1000)
                                        hiddenBtnAlpha -= 0.1f
                                    }
                                }
                            }
                        }
                    }, tint = Color.Unspecified
            )
        }
    }
}

/**按钮隐藏协程*/
private var hiddenJob: Job? = null

/**隐藏按钮透明度*/
private var hiddenBtnAlpha by mutableFloatStateOf(1f)

/**按钮组配置
 * @param iconId 资源id
 * @param iconTint 资源颜色过滤
 * @param title 按钮名
 * @param titleColor 按钮色
 * @param onClick 点击事件*/
private data class BtnConfig(
    val iconId: Int,
    val iconTint: Color,
    val title: ToolType,
    val titleColor: Color = Color.Black,
    val onClick: () -> Unit = { curTool = title }
)

private val btnClmMod = Modifier
    .alpha(0.8f)
    .padding(2.dp)
    .size(45.dp)

@Composable
private fun BtnConfig.Show() {
    Box(btnClmMod.clickable { onClick() }) {
        Space(if (title == curTool) Color(0xFF6D31F8) else Color.White, 0.3f)
        Column(Modifier.align(Alignment.Center)) {
            Icon(
                painterResource(id = iconId), null, tint = iconTint,
                modifier = Modifier
                    .size(30.dp)
                    .align(Alignment.CenterHorizontally)
            )
            Text(
                text = title.name, fontSize = 10.sp, color = titleColor,
                modifier = Modifier.align(Alignment.CenterHorizontally)
            )
        }
    }
}

@Preview(widthDp = 1000, heightDp = 480)
@Composable
private fun Test() {
    Space(Color.LightGray)
    Buttons()
}